Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var request = _interopRequireWildcard(require("./request")), _util = _interopRequireDefault(require("./util")), _debug = _interopRequireDefault(require("../../utils/debug"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _interopRequireWildcard(e) {
    if (e && e.__esModule) return e;
    var r = {};
    if (null != e) for (var t in e) if (Object.prototype.hasOwnProperty.call(e, t)) {
        var n = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(e, t) : {};
        n.get || n.set ? Object.defineProperty(r, t, n) : r[t] = e[t];
    }
    return r.default = e, r;
}

function _toConsumableArray(e) {
    return _arrayWithoutHoles(e) || _iterableToArray(e) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(e) {
    if (Symbol.iterator in Object(e) || "[object Arguments]" === Object.prototype.toString.call(e)) return Array.from(e);
}

function _arrayWithoutHoles(e) {
    if (Array.isArray(e)) {
        for (var r = 0, t = new Array(e.length); r < e.length; r++) t[r] = e[r];
        return t;
    }
}

function _typeof(e) {
    return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
        return typeof e;
    } : function(e) {
        return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
    })(e);
}

function _objectSpread(r) {
    for (var e = 1; e < arguments.length; e++) {
        var t = null != arguments[e] ? arguments[e] : {}, n = Object.keys(t);
        "function" == typeof Object.getOwnPropertySymbols && (n = n.concat(Object.getOwnPropertySymbols(t).filter(function(e) {
            return Object.getOwnPropertyDescriptor(t, e).enumerable;
        }))), n.forEach(function(e) {
            _defineProperty(r, e, t[e]);
        });
    }
    return r;
}

function _defineProperty(e, r, t) {
    return r in e ? Object.defineProperty(e, r, {
        value: t,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[r] = t, e;
}

function _slicedToArray(e, r) {
    return _arrayWithHoles(e) || _iterableToArrayLimit(e, r) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(e, r) {
    var t = [], n = !0, o = !1, a = void 0;
    try {
        for (var i, u = e[Symbol.iterator](); !(n = (i = u.next()).done) && (t.push(i.value), 
        !r || t.length !== r); n = !0) ;
    } catch (e) {
        o = !0, a = e;
    } finally {
        try {
            n || null == u.return || u.return();
        } finally {
            if (o) throw a;
        }
    }
    return t;
}

function _arrayWithHoles(e) {
    if (Array.isArray(e)) return e;
}

function _classCallCheck(e, r) {
    if (!(e instanceof r)) throw new TypeError("Cannot call a class as a function");
}

function _defineProperties(e, r) {
    for (var t = 0; t < r.length; t++) {
        var n = r[t];
        n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), 
        Object.defineProperty(e, n.key, n);
    }
}

function _createClass(e, r, t) {
    return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), e;
}

var Api = function() {
    function b(e, r) {
        _classCallCheck(this, b), this.$register(e, r), this.$requestInterceptors = b.$requestInterceptors, 
        this.$responseInterceptors = b.$responseInterceptors;
    }
    return _createClass(b, null, [ {
        key: "concatInterceptor",
        value: function() {
            var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : [], r = 1 < arguments.length ? arguments[1] : void 0;
            return r ? e.concat(r) : e;
        }
    } ]), _createClass(b, [ {
        key: "$register",
        value: function(e, r) {
            Object.assign(this, this.$generate(e, r));
        }
    }, {
        key: "$generate",
        value: function() {
            var l = this, p = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {}, f = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "", y = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : "/", d = {};
            return Object.keys(p).forEach(function(e) {
                var r = p[e];
                if ("string" == typeof r && (r = [ r ]), Array.isArray(r)) {
                    var t = _slicedToArray(r, 3), n = t[0], o = void 0 === n ? "get" : n, a = t[1], i = void 0 === a ? {} : a, u = t[2], s = void 0 === u ? {} : u, c = "".concat(i.host ? i.host : f).concat(y).concat(e);
                    d[e] = function() {
                        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {}, r = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
                        (r = _objectSpread({}, i, r)).isData && (e = {
                            data: e
                        }), (e = _objectSpread({}, s, e, {
                            url: c,
                            method: o
                        })).data = Object.assign({}, s.data, e.data), e.param && (e.param = Object.assign({}, s.param, e.param), 
                        e.url += "?".concat(_util.default.serialize(e.param)));
                        var t = b.concatInterceptor(l.$requestInterceptors, r.requestInterceptors), n = b.concatInterceptor(l.$responseInterceptors, r.responseInterceptors);
                        return l.$runResolvers(t, e).then(function(e) {
                            e.method = e.method.toLowerCase ? e.method.toLowerCase() : "";
                            var r = JSON.stringify(e), t = "upload" === e.method ? request.upload(e) : request.request(e);
                            return t.catch(function(e) {
                                return l.$errorHandler(e, r);
                            }), t.then(function(e) {
                                return l.$responseHandler(e, r);
                            }).then(function(e) {
                                return l.$runResolvers(n, e);
                            });
                        });
                    }, d[e].url = c, i.alias && (d[i.alias] = d[e]);
                } else "object" === _typeof(r) && (d[e] = l.$generate(r, f, "".concat(y).concat(e, "/")));
            }), d;
        }
    }, {
        key: "$runResolvers",
        value: function(e, r) {
            var t = (e = _toConsumableArray(e))[0];
            return t ? (e.shift(), t(r).then(this.$runResolvers.bind(this, e))) : Promise.resolve(r);
        }
    }, {
        key: "$responseHandler",
        value: function(e, r) {
            return e.code && _debug.default.error({
                e: e,
                type: "api",
                msg: r
            }), Promise.resolve(e);
        }
    }, {
        key: "$errorHandler",
        value: function(e, r) {
            return _debug.default.error({
                e: e,
                type: "api",
                msg: r
            }), _util.default.requestFailTip(e), Promise.reject(e);
        }
    } ]), b;
}();

Api.$requestInterceptors = [], Api.$responseInterceptors = [], Api.interceptors = {
    request: function(e) {
        Api.$requestInterceptors = Api.$requestInterceptors.concat(Array.isArray(e) ? e : [ e ]);
    },
    response: function(e) {
        Api.$responseInterceptors = Api.$responseInterceptors.concat(Array.isArray(e) ? e : [ e ]);
    }
};

var _default = Api;

exports.default = _default;